package attendance.gencode;

import java.util.HashMap;
import java.util.Map;

import org.mirrentools.sd.ScrewDriver;
import org.mirrentools.sd.SdType;
import org.mirrentools.sd.models.SdBean;
import org.mirrentools.sd.models.SdColumn;
import org.mirrentools.sd.models.SdTemplate;
import org.mirrentools.sd.options.ScrewDriverOptions;
import org.mirrentools.sd.options.SdDatabaseOptions;

import io.vertx.core.json.JsonObject;

public class CreateRecord {
	public static void main(String[] args) throws Exception {
		String packageName = "com.zhunei.attendance.";
		String entityName = "Record";
		String templatePath = "vertx";
		SdBean bean = new SdBean();
		bean.setName(entityName).setRemark("考勤记录表");
		bean.addColumn(new SdColumn().setName("id").setType(SdType.LONG).setPrimary(true).setAutoIncrement(true).setRemark("记录的id"));
		bean.addColumn(new SdColumn().setName("uid").setType(SdType.STRING).setNullable(false).setLength(32).setRemark("用户的id"));
		bean.addColumn(new SdColumn().setName("years").setType(SdType.INTEGER).setNullable(false).setRemark("那一年"));
		bean.addColumn(new SdColumn().setName("months").setType(SdType.INTEGER).setNullable(false).setRemark("那一月"));
		bean.addColumn(new SdColumn().setName("days").setType(SdType.INTEGER).setNullable(false).setRemark("那一日"));
		bean.addColumn(new SdColumn().setName("start").setType(SdType.LONG).setNullable(false).setRemark("工作时间"));
		bean.addColumn(new SdColumn().setName("end").setType(SdType.LONG).setRemark("休息时间"));
		bean.addColumn(new SdColumn().setName("wtime").setType(SdType.INTEGER).setDefaults("0").setRemark("工作时长"));

		Map<String, SdTemplate> templates = new HashMap<String, SdTemplate>();
		templates.put("entity", new SdTemplate().setPath(templatePath).setFile("Entity.ftl")// Lombok模板为EntityLombok.ftl
				.setPackageName(packageName + "entity").setClassName(entityName));
		templates.put("sql", new SdTemplate().setPath(templatePath).setFile("SQL.ftl").setPackageName(packageName + "sql").setClassName(entityName + "SQL"));
		templates.put("service", new SdTemplate().setPath(templatePath).setFile("ServiceEmpty.ftl").setPackageName(packageName + "service").setClassName(entityName + "Service"));
		templates.put("serviceImpl", new SdTemplate().setPath(templatePath).setFile("ServiceImplEmpty.ftl").setPackageName(packageName + "service.impl").setClassName(entityName + "ServiceImpl"));
		templates.put("router", new SdTemplate().setPath(templatePath).setFile("RouterEmpty.ftl").setPackageName(packageName + "router").setClassName(entityName + "Router"));
		JsonObject config = Config.getDbConfig();
		String driverClass = config.getString("druid.driver");
		String url = config.getString("druid.url");
		String user = config.getString("druid.username");
		String password = config.getString("druid.password");
		SdDatabaseOptions databaseOptions = new SdDatabaseOptions(driverClass, url, user, password);
		ScrewDriver instance = ScrewDriver.instance(new ScrewDriverOptions(templates, databaseOptions));
		instance.createCode(bean);
		instance.createTable(bean);
	}
}
